﻿using AutoMapper;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.EntityFrameworkCore;
using Rj_GsInsurance.Server.DataBaseContext;
using Rj_GsInsurance.Server.DataBaseContext.Entities;
using Rj_GsInsurance.Server.Extensions;
using Rj_GsInsurance.Server.Models;
using Rj_GsInsurance.Server.Models.InsurMsgDataInput;
using Rj_GsInsurance.Server.Models.InsurMsgDataOutput;
using Rj_GsInsurance.Server.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Wpf.Ui;
using Wpf.Ui.Controls;

namespace Rj_GsInsurance.Client.ViewModels
{
    public partial class DocUploadViewModel: ObservableObject
    {
        /// <summary>
        /// 未上传医生
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<DocUploadInput> _doctorList;
        /// <summary>
        /// 已上传医生
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<QueryDoctorOutput> _queryDoctorList;
        [ObservableProperty]
        private DocUploadInput _selectDoctor;
        /// <summary>
        /// 查询工号
        /// </summary>
        [ObservableProperty]
        private string _userCode;
        /// <summary>
        /// 医师编号
        /// </summary>
        [ObservableProperty]
        private string _doctorCode;
        /// <summary>
        /// 医师姓名
        /// </summary>
        [ObservableProperty]
        private string _doctorName;
        /// <summary>
        /// 科室编号
        /// </summary>
        [ObservableProperty]
        private string _deptCode;
        [ObservableProperty]
        private string _inputXszw;
        [ObservableProperty]
        private string _inputByyx;
        [ObservableProperty]
        private string _inputLxdh;
        [ObservableProperty]
        private string _inputZzjbnr;
        [ObservableProperty]
        private string _inputJbzl;

        [ObservableProperty]
        private ObservableCollection<SystemTbDepartment> _deptList;
        [ObservableProperty]
        private SystemTbDepartment _selectedDeptList;
        [ObservableProperty]
        private List<InsurTypeCode> _insurDictYsjb = new List<InsurTypeCode>()
        {
            new InsurTypeCode("00","职业医师"),
            new InsurTypeCode("01","职业助理医师"),
        };
        [ObservableProperty]
        private InsurTypeCode _selectedInsurDictYsjb;
        [ObservableProperty]
        private List<InsurTypeCode> _insurDictCfq = new List<InsurTypeCode>()
        {
            new InsurTypeCode("0","不可以录入处方"),
            new InsurTypeCode("1","可以录入处方"),
        };
        [ObservableProperty]
        private InsurTypeCode _selectedInsurDictCfq;
        [ObservableProperty]
        private List<InsurTypeCode> _insurDictZylb = new List<InsurTypeCode>()
        {
            new InsurTypeCode("1","临床"),
            new InsurTypeCode("2","中医"),
            new InsurTypeCode("3","口腔"),
            new InsurTypeCode("4","公共卫生"),
            new InsurTypeCode("5","中西药结合"),
        };
        [ObservableProperty]
        private InsurTypeCode _selectedInsurDictZylb;
        [ObservableProperty]
        private List<InsurTypeCode> _insurDictZyfw = new List<InsurTypeCode>()
        {
            new InsurTypeCode("01","内科专业"),
            new InsurTypeCode("02","外科专业"),
            new InsurTypeCode("03","妇产科专业"),
            new InsurTypeCode("04","眼耳鼻咽喉科"),
            new InsurTypeCode("05","医学影像和放射治疗"),
            new InsurTypeCode("06","精神卫生专业"),
            new InsurTypeCode("07","皮肤和性病专业"),
            new InsurTypeCode("08","口腔专业"),
            new InsurTypeCode("09","麻醉专业"),
            new InsurTypeCode("10","儿科专业"),
            new InsurTypeCode("11","中医专业"),
            new InsurTypeCode("12","中西医结合专业"),
            new InsurTypeCode("13","康复医学专业"),
            new InsurTypeCode("14","重症医学科专业"),
            new InsurTypeCode("15","医学检验与病理专业"),
            new InsurTypeCode("16","急救医学专业"),
            new InsurTypeCode("17","预防保健专业"),
            new InsurTypeCode("18","职业病专业"),
        };
        [ObservableProperty]
        private InsurTypeCode _selectedInsurDictZyfw;

        [ObservableProperty]
        private List<InsurTypeCode> _insurDictZc = new List<InsurTypeCode>()
        {
            new InsurTypeCode("231","主任医师"),
            new InsurTypeCode("232","副主任医师"),
            new InsurTypeCode("233","主治医师"),
            new InsurTypeCode("234","医师"),
            new InsurTypeCode("235","医士"),
            new InsurTypeCode("241","主任药师"),
            new InsurTypeCode("242","副主任药师"),
            new InsurTypeCode("243","主管药师"),
            new InsurTypeCode("244","药师"),
            new InsurTypeCode("245","药士"),
            new InsurTypeCode("251","主任护师"),
            new InsurTypeCode("252","副主任护师"),
            new InsurTypeCode("253","主管护师"),
            new InsurTypeCode("254","护师"),
            new InsurTypeCode("255","护士"),
            new InsurTypeCode("261","主任技师"),
            new InsurTypeCode("262","副主任技师"),
            new InsurTypeCode("263","主管技师"),
            new InsurTypeCode("264","技师"),
            new InsurTypeCode("265","技师士"),
        };
        [ObservableProperty]
        private InsurTypeCode _selectedInsurDictZc;
        [ObservableProperty]
        private List<InsurTypeCode> _insurDictXzzw = new List<InsurTypeCode>()
        {
            new InsurTypeCode("101","国家级正职"),
            new InsurTypeCode("102","国家级副职"),
            new InsurTypeCode("111","省部级正职"),
            new InsurTypeCode("112","省部级副职"),
            new InsurTypeCode("121","厅局级正职"),
            new InsurTypeCode("122","厅局级副职"),
            new InsurTypeCode("131","县处级正职"),
            new InsurTypeCode("132","县处级副职"),
            new InsurTypeCode("141","乡科级正职"),
            new InsurTypeCode("142","乡科级副职"),
            new InsurTypeCode("150","科员级"),
            new InsurTypeCode("160","办事员级"),
            new InsurTypeCode("199","未定职"),
        };
        [ObservableProperty]
        private InsurTypeCode _selectedInsurDictXzzw;

        private IGsInsuranceServer GsInsuranceServer;
        private HisDbContext HisDbContext;
        private IMapper Mapper;

        public ISnackbarService SnackbarService { get; set; }

        public DocUploadViewModel(IGsInsuranceServer gsInsuranceServer, ISnackbarService snackbarService, HisDbContext hisDbContext, IMapper mapper)
        {
            GsInsuranceServer = gsInsuranceServer;
            SnackbarService = snackbarService;
            HisDbContext = hisDbContext;
            Mapper = mapper;
            var workers = HisDbContext.SystemTbWorkers.ToList();
            DoctorList = Mapper.Map<ObservableCollection<DocUploadInput>>(workers);
            DeptList= new ObservableCollection<SystemTbDepartment>(HisDbContext.SystemTbDepartments.ToList());
            //_doctorList = gsInsuranceServer.GetDocUploadByExample(a =>
            //{
            //    if (!string.IsNullOrWhiteSpace(_userCode))
            //        return a.Code == _userCode;
            //    return true;
            //}).Result;
        }

        [RelayCommand]
        private void OnUpload(object param)
        {
            DocUploadInput  input= param as DocUploadInput;
            try
            {
                var insurMsgOut = input.Excute<DocUploadInput, EmptyOutput>("3400");
            }
            catch (Exception ex)
            {
                SnackbarService.Show(
                    "警告!",
                    $"医保交易失败！{ex.Message}",
                    ControlAppearance.Danger,
                    new SymbolIcon(SymbolRegular.Fluent24),
                    TimeSpan.FromSeconds(30)
                );
            }
        }
        /// <summary>
        /// 全部上传
        /// </summary>
        /// <param name="param"></param>
        [RelayCommand]
        private void OnUploadAll(object param)
        {
            if (SelectDoctor == null)
            {
                SnackbarService.Show(
                    "警告!",
                    $"请选择医生",
                    ControlAppearance.Danger,
                    new SymbolIcon(SymbolRegular.Fluent24),
                    TimeSpan.FromSeconds(30)
                );
                return;
            }

            var input = SelectDoctor;
            input.Cfq =SelectedInsurDictCfq?.Code;
            input.Ksbh= SelectedDeptList?.DeptCode;
            input.Ksbh= SelectedDeptList?.DeptName;
            input.Yszyzh= SelectDoctor.Yszyzh;
            input.Ysjb = SelectedInsurDictYsjb.Code;
            input.Zc=SelectedInsurDictZc?.Code;
            input.Zxzw = SelectedInsurDictXzzw?.Code;
            input.Xszw= InputXszw;
            input.Byyx= InputByyx;
            input.Xb= SelectDoctor.Xb;
            input.Lxdh= InputLxdh;
            input.Sfzh= SelectDoctor.Sfzh;
            input.Zzjbnr= InputZzjbnr;
            input.Jbzl= InputJbzl;
            input.Memo = "";
            input.Jbr = "管理员";
            input.Jbsj = DateTime.Now.ToString("yyyyMMddHHmmss");
            input.Zylb= SelectedInsurDictZylb?.Code;
            input.Zyfw= SelectedInsurDictZyfw?.Code;
            input.Xyjgnbbm= SelectDoctor.Xyjgnbbm;

            try
            {
                var insurMsgOut = input.Excute<DocUploadInput, EmptyOutput>("3400");
            }
            catch (Exception ex)
            {
                SnackbarService.Show(
                    "警告!",
                    $"医保交易失败！{ex.Message}",
                    ControlAppearance.Danger,
                    new SymbolIcon(SymbolRegular.Fluent24),
                    TimeSpan.FromSeconds(30)
                );
                return;
            }

            SnackbarService.Show(
                    "提示!",
                    $"上传成功!",
                    ControlAppearance.Success,
                    new SymbolIcon(SymbolRegular.Fluent24),
                    TimeSpan.FromSeconds(30)
                );
        }
        /// <summary>
        /// 查询本地未上传
        /// </summary>
        /// <param name="param"></param>
        [RelayCommand]
        private async void OnQuery(object param)
        {
            var insur = await HisDbContext.SystemTbWorkers.Where(e => string.IsNullOrWhiteSpace(UserCode) || e.WorkerId.ToString() == UserCode).ToListAsync();
            DoctorList= Mapper.Map<ObservableCollection<DocUploadInput>>(insur);
            foreach (var item in DoctorList)
            {
                item.Ysjb = "00";
                item.Cfq = "1";
            }
            //DoctorList =await GsInsuranceServer.GetDocUploadByExample(e => string.IsNullOrWhiteSpace(UserCode) || e.Code == UserCode);
        }
        /// <summary>
        /// 上传结果查询
        /// </summary>
        /// <param name="param"></param>
        [RelayCommand]
        private void OnQueryInsur(object param)
        {
            QueryDoctorInput input = new QueryDoctorInput()
            {
                Code = DoctorCode,
                Name = DoctorName,
                DeptCode = DeptCode,
            };
            try
            {
                var insurMsgOut = input.Excute<QueryDoctorInput, List<QueryDoctorOutput>>("1104");
                QueryDoctorList = new ObservableCollection<QueryDoctorOutput>(insurMsgOut.output);
            }
            catch (Exception ex)
            {
                SnackbarService.Show(
                    "警告!",
                    $"医保交易失败！{ex.Message}",
                    ControlAppearance.Danger,
                    new SymbolIcon(SymbolRegular.Fluent24),
                    TimeSpan.FromSeconds(30)
                );
            }
        }
        /// <summary>
        /// 撤销
        /// </summary>
        /// <param name="param"></param>
        [RelayCommand]
        private void OnRevoke(object param)
        {

        }
    }

    
}
